package tree;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author wcj
 * @Date 2021/7/6 23:51
 * @Version 1.0
 */
public class ErgodicTree {

    private static List<Integer> res = new ArrayList<>();

    /**
     * 前序遍历的递归
     *
     * @param root
     * @return
     */
    public static void preorder(TreeNode root) {
        if (root == null) {
            return;
        }
        res.add(root.val);
        preorder(root.left);
        preorder(root.right);
    }

    /**
     * 前序遍历的非递归
     *
     * @param root
     * @return
     */
    public static List<Integer> preOrder(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode tmp = root;
        while (tmp != null || !stack.isEmpty()) {
            if (tmp != null) {
                res.add(tmp.val);
                stack.add(tmp);
                tmp = tmp.left;
            } else {
                tmp = stack.pop();
                tmp = tmp.right;
            }
        }
        return res;
    }

    /**
     * 中序遍历的递归
     *
     * @param root
     * @return
     */
    public static void inorder(TreeNode root) {
        if (root == null) {
            return;
        }
        preorder(root.left);
        res.add(root.val);
        preorder(root.right);
    }

    /**
     * 中序遍历的非递归
     *
     * @param root
     * @return
     */
    public static List<Integer> inOrder(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode tmp = root;
        while (tmp != null || !stack.isEmpty()) {
            if (tmp != null) {
                stack.add(tmp);
                tmp = tmp.left;
            } else {
                tmp = stack.pop();
                res.add(tmp.val);
                tmp = tmp.right;
            }
        }
        return res;
    }

    /**
     * 后序序遍历的递归
     *
     * @param root
     * @return
     */
    public static void afterOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        afterOrder(root.left);
        afterOrder(root.right);
        res.add(root.val);
    }

    /**
     * 后序遍历的非递归
     *
     * @param root
     * @return
     */
    public static List<Integer> afterOrder2(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode tmp = root;
        TreeNode p = null;
        // 先走到最左下角
        while (tmp != null) {
            stack.add(tmp);
            tmp = tmp.left;
        }
        // 开启后序遍历流程
        while (!stack.isEmpty()) {
            tmp = stack.pop();
            if (tmp.right == null || tmp.right == p) {
                res.add(tmp.val);
                p = tmp;
            } else {
                stack.add(tmp);
                tmp = tmp.right;
                while (tmp != null) {
                    stack.add(tmp);
                    tmp = tmp.left;
                }
            }
        }
        return res;
    }
}
